class Game {
    constructor() {
        this.canvas = document.getElementById('gameCanvas');
        this.ctx = this.canvas.getContext('2d');
        this.score = 0;
        this.isGamePaused = true;
        
        // 设置画布尺寸
        this.resizeCanvas();
        window.addEventListener('resize', () => this.resizeCanvas());
        
        // 初始化游戏对象
        this.player = {
            x: this.canvas.width / 2,
            y: this.canvas.height - 50,
            width: 40,
            height: 40,
            speed: 5
        };
        
        this.bullets = [];
        this.enemies = [];
        this.keys = {};
        
        // 事件监听
        this.setupEventListeners();
        
        // 开始游戏循环
        this.gameLoop();
    }
    
    resizeCanvas() {
        this.canvas.width = this.canvas.offsetWidth;
        this.canvas.height = this.canvas.offsetHeight;
    }
    
    setupEventListeners() {
        window.addEventListener('keydown', (e) => {
            this.keys[e.key] = true;
            if (e.code === 'Space') {
                this.isGamePaused = !this.isGamePaused;
                e.preventDefault();
            }
        });
        
        window.addEventListener('keyup', (e) => {
            this.keys[e.key] = false;
        });
    }
    
    update() {
        if (this.isGamePaused) return;
        
        // 更新玩家位置
        if (this.keys['ArrowLeft']) {
            this.player.x = Math.max(0, this.player.x - this.player.speed);
        }
        if (this.keys['ArrowRight']) {
            this.player.x = Math.min(this.canvas.width - this.player.width, this.player.x + this.player.speed);
        }
        
        // 生成敌人
        if (Math.random() < 0.02) {
            this.enemies.push({
                x: Math.random() * (this.canvas.width - 30),
                y: -30,
                width: 30,
                height: 30,
                speed: 2,
                color: `hsl(${Math.random() * 360}, 50%, 50%)`  // 随机颜色
            });
        }
        
        // 更新子弹
        if (Math.random() < 0.1) {
            this.bullets.push({
                x: this.player.x + this.player.width / 2 - 2,
                y: this.player.y,
                width: 4,
                height: 10,
                speed: 7
            });
        }
        
        // 移动子弹
        this.bullets.forEach((bullet, index) => {
            bullet.y -= bullet.speed;
            if (bullet.y < 0) {
                this.bullets.splice(index, 1);
            }
        });
        
        // 移动敌人
        this.enemies.forEach((enemy, index) => {
            enemy.y += enemy.speed;
            if (enemy.y > this.canvas.height) {
                this.enemies.splice(index, 1);
            }
        });
        
        // 碰撞检测
        this.checkCollisions();
    }
    
    checkCollisions() {
        this.bullets.forEach((bullet, bulletIndex) => {
            this.enemies.forEach((enemy, enemyIndex) => {
                if (this.isColliding(bullet, enemy)) {
                    this.addExplosion(enemy.x, enemy.y, enemy.color);
                    this.bullets.splice(bulletIndex, 1);
                    this.enemies.splice(enemyIndex, 1);
                    this.score += 100;
                    document.getElementById('scoreValue').textContent = this.score;
                }
            });
        });
    }
    
    isColliding(rect1, rect2) {
        return rect1.x < rect2.x + rect2.width &&
               rect1.x + rect1.width > rect2.x &&
               rect1.y < rect2.y + rect2.height &&
               rect1.y + rect1.height > rect2.y;
    }
    
    addExplosion(x, y, color) {
        const particles = [];
        for (let i = 0; i < 15; i++) {
            const angle = (Math.PI * 2 / 15) * i;
            particles.push({
                x: x + 15,
                y: y + 15,
                radius: 3,
                speed: 3,
                dx: Math.cos(angle),
                dy: Math.sin(angle),
                alpha: 1,
                color: color
            });
        }
        
        const animate = () => {
            particles.forEach((particle, index) => {
                particle.x += particle.dx * particle.speed;
                particle.y += particle.dy * particle.speed;
                particle.alpha -= 0.02;
                
                if (particle.alpha > 0) {
                    this.ctx.beginPath();
                    this.ctx.arc(particle.x, particle.y, particle.radius, 0, Math.PI * 2);
                    this.ctx.fillStyle = `hsla(${particle.color}, ${particle.alpha})`;
                    this.ctx.fill();
                    requestAnimationFrame(animate);
                }
            });
        };
        
        animate();
    }
    
    draw() {
        this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
        
        // 绘制星空背景
        this.drawStarryBackground();
        
        // 绘制玩家
        this.ctx.fillStyle = '#00ff00';
        this.ctx.beginPath();
        this.ctx.moveTo(this.player.x + this.player.width / 2, this.player.y);
        this.ctx.lineTo(this.player.x + this.player.width, this.player.y + this.player.height);
        this.ctx.lineTo(this.player.x, this.player.y + this.player.height);
        this.ctx.closePath();
        this.ctx.fill();
        
        // 绘制子弹
        this.ctx.fillStyle = '#fff';
        this.bullets.forEach(bullet => {
            this.ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
        });
        
        // 绘制敌人
        this.enemies.forEach(enemy => {
            this.ctx.fillStyle = enemy.color;
            this.ctx.beginPath();
            this.ctx.moveTo(enemy.x + enemy.width / 2, enemy.y + enemy.height);
            this.ctx.lineTo(enemy.x + enemy.width, enemy.y);
            this.ctx.lineTo(enemy.x, enemy.y);
            this.ctx.closePath();
            this.ctx.fill();
        });
        
        // 绘制暂停状态
        if (this.isGamePaused) {
            this.ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
            this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
            this.ctx.fillStyle = '#fff';
            this.ctx.font = '30px Arial';
            this.ctx.textAlign = 'center';
            this.ctx.fillText('按空格键开始', this.canvas.width / 2, this.canvas.height / 2);
        }
    }
    
    drawStarryBackground() {
        // 绘制渐变背景
        const gradient = this.ctx.createLinearGradient(0, 0, 0, this.canvas.height);
        gradient.addColorStop(0, '#000033');
        gradient.addColorStop(1, '#000066');
        this.ctx.fillStyle = gradient;
        this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
        
        // 绘制星星
        for (let i = 0; i < 100; i++) {
            const x = Math.random() * this.canvas.width;
            const y = Math.random() * this.canvas.height;
            const radius = Math.random() * 1.5;
            const opacity = Math.random();
            
            this.ctx.beginPath();
            this.ctx.arc(x, y, radius, 0, Math.PI * 2);
            this.ctx.fillStyle = `rgba(255, 255, 255, ${opacity})`;
            this.ctx.fill();
        }
    }
    
    gameLoop() {
        this.update();
        this.draw();
        requestAnimationFrame(() => this.gameLoop());
    }
}

// 初始化游戏
window.onload = () => {
    new Game();
}; 